"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = checkModel;

var _logTips = _interopRequireDefault(require("log-tips"));

var _utils = require("./utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function checkModel(model, existModels) {
  var namespace = model.namespace,
      reducers = model.reducers,
      effects = model.effects,
      subscriptions = model.subscriptions; // namespace 必须被定义

  (0, _logTips["default"])(namespace, '[app.model] namespace should be defined'); // 并且是字符串

  (0, _logTips["default"])(typeof namespace === 'string', "[app.model] namespace should be string, but got ".concat(_typeof(namespace))); // 并且唯一

  (0, _logTips["default"])(!existModels.some(function (model) {
    return model.namespace === namespace;
  }), '[app.model] namespace should be unique'); // state 可以为任意值
  // reducers 可以为空，PlainObject 或者数组

  if (reducers) {
    (0, _logTips["default"])((0, _utils.isPlainObject)(reducers) || (0, _utils.isArray)(reducers), "[app.model] reducers should be plain object or array, but got ".concat(_typeof(reducers))); // 数组的 reducers 必须是 [Object, Function] 的格式

    (0, _logTips["default"])(!(0, _utils.isArray)(reducers) || (0, _utils.isPlainObject)(reducers[0]) && (0, _utils.isFunction)(reducers[1]), '[app.model] reducers with array should be [Object, Function]');
  } // effects 可以为空，PlainObject


  if (effects) {
    (0, _logTips["default"])((0, _utils.isPlainObject)(effects), "[app.model] effects should be plain object, but got ".concat(_typeof(effects)));
  }

  if (subscriptions) {
    // subscriptions 可以为空，PlainObject
    (0, _logTips["default"])((0, _utils.isPlainObject)(subscriptions), "[app.model] subscriptions should be plain object, but got ".concat(_typeof(subscriptions))); // subscription 必须为函数

    (0, _logTips["default"])(isAllFunction(subscriptions), '[app.model] subscription should be function');
  }
}

function isAllFunction(obj) {
  return Object.keys(obj).every(function (key) {
    return (0, _utils.isFunction)(obj[key]);
  });
}